# -*- encoding:utf-8 -*-
import datetime
import astral
from database.models.MasterInfo import MasterInfo
from database.models.DatabaseCommon import *

class sun_run_pred(object):
    def __init__(self):
        pass

    @staticmethod
    def getAbsPos():
        s = Session()
        c = s.query(MasterInfo).all()
        if c is not None and len(c) is 0:
            return 0,0
        lon = 0
        lati = 0
        for i in c:
            if i.name == u'longitude':
                lon = i.value
            if i.name == u'latitude':
                lati = i.value
        return lon, lati

    @staticmethod
    def get_sun_up2():
        da = datetime.date.today()
        l,la = sun_run_pred.getAbsPos()
        lf = float(l)
        return sun_run_pred.get_sun_up(float(l),float(la),da.year,da.month,da.day)

    @staticmethod
    def get_sun_up(longitude, latitude, year, month, day):
        my_date = datetime.date.today().replace(year=year, month=month, day=day)
        location_city_name = astral.Location(('city_name', 'China', latitude, longitude, 'Asia/Shanghai', 0))
        time_sunrise = location_city_name.sunrise(date=my_date, local=True)
        time_sunrise_update = time_sunrise + datetime.timedelta(hours=0, minutes=0, seconds=0)  # 提前/延后日出时间量
        time_sunset = location_city_name.sunset(date=my_date, local=True)
        time_sunset_update = time_sunset + datetime.timedelta(hours=0, minutes=0, seconds=0)  # 提前/延后日落时间量
        return time_sunrise_update, time_sunset_update

def main():
    print("in main")
    u, d = sun_run_pred.get_sun_up2()
    print('u',u)
    print('d',d)

if __name__ == '__main__':
    main()
